﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DiscoveryLogic.Analytics.BusinessLogic.Entities;
using DiscoveryLogic.Analytics.DataObjects;
using DiscoveryLogic.Analytics.DataObjects.Rules;
using DiscoveryLogic.Analytics.UI.Common.Wizard;
using DiscoveryLogic.Common.Utilities;

namespace DiscoveryLogic.Analytics.Modules.LinkRules
{
    /// <summary>
    /// Interaction logic for LinkRuleInputEntityFieldMappings.xaml
    /// </summary>
    public partial class LinkRuleInputEntityFieldMappings : UserControl, IWizardPageBase
    {
        private Wizard _Owner = null;

        public LinkRuleInputEntityFieldMappings()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            this.cboSrcEntityType.Items.Clear();
            foreach(EntityType entityType in Enum.GetValues(typeof(EntityType)))
            {
                this.cboSrcEntityType.Items.Add(entityType.ToString());
            }
            this.cboDbProvider.Items.Clear();
            this.cboDbProvider.Items.Add(DataProviderType.MSSQL.ToString());
            this.cboDbProvider.Items.Add(DataProviderType.Oracle.ToString());

            EntityRelationRule linkRule =
                (EntityRelationRule) this._Owner.SharedVariables["LinkRule"];
            this.cboSrcEntityType.Text = linkRule.SrcEntityType.ToString();
            this.txtRuleName.Text = linkRule.RuleName;
            this.cboSrcEntitySets.Text = linkRule.SrcEntitySetName;
            this.txtConnStr.Text = linkRule.ConnStr;
            this.txtSQL.Text = linkRule.Sql;
            this.cboDbProvider.Text = linkRule.DbProvider.ToString();
        }

        private void cboSrcEntityType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (!this.IsInitialized)
                return;

            if (this.cboSrcEntityType.SelectedIndex >= 0)
            {
                EntityType entityType =
                    (EntityType) Enum.Parse(typeof (EntityType), this.cboSrcEntityType.SelectedItem.ToString());
                this.cboSrcEntitySets.Items.Clear();
                List<EntityDataSet> entitySets = DataSetManager.GetEntityDataSets(entityType);
                if (entitySets != null && entitySets.Count > 0)
                {
                    foreach (EntityDataSet set in entitySets)
                    {
                        this.cboSrcEntitySets.Items.Add(set.DataSetName);
                    }
                }
            }
        }

        #region Implementation of IWizardPageBase

        public event OnBeforeMoveToNext BeforeMoveToNext;
        public event OnBeforeDisplay BeforeDisplay;
        private IWizardPageBase _PrevPage;
        public IWizardPageBase PrevPage
        {
            get
            {
                return _PrevPage;
            }
            set
            {
                _PrevPage = value;
            }
        }

        private IWizardPageBase _NextPage;
        public IWizardPageBase NextPage
        {
            get
            {
                return _NextPage;
            }
            set
            {
                _NextPage = value;
            }
        }

        private string _Title = "Data source";
        public string Title
        {
            get
            {
                return _Title;
            }
            set
            {
                _Title = value;
            }
        }

        private string _Desc = "Specify how target data is retrieved using SQL and parameters";
        public string Description
        {
            get
            {
                return _Desc;
            }
            set
            {
                _Desc = value;
            }
        }

        public void Init(Wizard owner)
        {
            this._Owner = owner;
        }

        public bool MoveNextPage()
        {
            try
            {
                EntityRelationRule rule =
                    this._Owner.SharedVariables["LinkRule"] as EntityRelationRule;
                rule.RuleName = this.txtRuleName.Text;
                rule.SrcEntityType = (EntityType) Enum.Parse(typeof (EntityType), this.cboSrcEntityType.SelectedItem.ToString());
                rule.FieldNameMappings=new Dictionary<string, string>();
                PropertyInfo[] srcProps = EntityTypeUtil.GetProperties(rule.SrcEntityType);
                foreach(PropertyInfo prop in srcProps)
                {
                    rule.FieldNameMappings.Add(prop.Name,prop.Name);
                }
                rule.SrcEntitySetName = this.cboSrcEntitySets.Text;
                rule.DbProvider = (DataProviderType)Enum.Parse(typeof(DataProviderType), this.cboDbProvider.SelectedItem.ToString());
                rule.ConnStr = this.txtConnStr.Text;
                rule.Sql = this.txtSQL.Text;
                if (this.BeforeMoveToNext != null)
                    this.BeforeMoveToNext(new WizardPageNavigationEventArgs(this._Owner, false));
                return true;
            }
            catch(Exception ex)
            {
                ExceptionHandler.HandlesException(ex);
                MessageBox.Show(ex.Message);
                return false;
            }
        }
        #endregion
    }
}
